//###############
// FILE : CanRunAction.java
// WRITER : Lior Zimmerman liorz06 066462300
// WRITER : Ohad Dan ohad_dan 036767960
// EXERCISE : oop ex3 2008
// DESCRIPTION:
// Checks if all parameters are fine for search.
//###############
package oop.ex3.fileSearchController;

import oop.ex3.fileSearchView.GUIView;
import oop.ex3.fileSearchView.SearchTabInterface;
import oop.ex3.fileSearchView.SetupTabInterface;
/**
 * Checks if all parameters are fine for search.
 */
public class CanRunAction extends Controller {
	/**
	 * Construct a new CanRunAction checker.
	 * @param frame Main frame of the program.
	 */
    public CanRunAction(GUIView frame) {
        super(frame);
        
    }
    /**
     * Checks if setup tab is defined for running.
     * @return True if the setup tab is configured correctly for running.
     */
    public boolean setupValid() {
        SetupTabInterface setupView = _gui.getSetupView();
        if ( ( setupView.rdoToFileSelected() || 
                setupView.rdoToFileScreenSelected() ) && 
                !setupView.txtGetOutput().isEmpty()) {
            return true;
        }else if ( setupView.rdoToScreenSelected() ) {
            return true;
        }
        return false;

    }
    //true for text mode
    public void setFileTextModes(boolean val) {
        _gui.getSearchView().btnFilterFileSetEnabled(!val);
        _gui.getSearchView().txtFilterFileSetEnabled(!val);
        _gui.getSearchView().txtFilterExpSetEnabled(val);

    }
    /**
     * Set run button status according to the current data.
     */
    public void doAction() {
      
      //check if workdir is not empty
      SearchTabInterface searchView = _gui.getSearchView();
      setFileTextModes(searchView.rdoFilterFromTextSelected());
      if( searchView.txtWorkDirGetText().isEmpty() ){
          searchView.btnRunSetEnabled(false);
          return;
      }
      //check if filter from text selected
      if ( searchView.rdoFilterFromTextSelected() ) {
          //check if legal to run:
          if( !searchView.txtFilterExpGetText().isEmpty() && setupValid() ){
              searchView.btnRunSetEnabled(true);
              return;
          }
      }else if ( searchView.rdoFilterFromFileSelected() ){
          if ( !searchView.txtFilterFileGetText().isEmpty() && setupValid() ){
              searchView.btnRunSetEnabled(true);
              return;
          }
      }
      searchView.btnRunSetEnabled(false);
      return;
    }
}
